package com.example.tianling.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.tianling.admin.service.ActivateService;
import com.example.tianling.admin.service.GoodService;
import com.example.tianling.admin.service.UserDataService;
import com.example.tianling.admin.vo.AdminVo;
import com.example.tianling.admin.vo.UserDataVo;
import com.example.tianling.admin.vo.userDetailDataVo;
import com.example.tianling.common.annotation.Login;
import com.example.tianling.common.annotation.LoginUser;
import com.example.tianling.common.utils.R;
import com.example.tianling.dao.UserMapper;
import com.example.tianling.entity.*;
import com.example.tianling.service.ArticleService;
import com.example.tianling.service.CommentService;
import com.example.tianling.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
public class AdminShopController {

    @Autowired
    private GoodService goodService;

    @Login
    @PostMapping("/admin/uploadGoods.do")
    @ResponseBody
    public R uploadGoods(@RequestBody ProductEntity productEntity){

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化时间
        String formattedDateTime = now.format(formatter);
        productEntity.setCreateTime(formattedDateTime);
        return R.success(goodService.uploadGood(productEntity));
    }

    //获取所有商品
    @Login
    @PostMapping("/admin/getGoods.do")
    @ResponseBody
    public R getGoods(){
        return R.success(goodService.getGood());
    }

    //获取单个商品
    @Login
    @PostMapping("/admin/getOneGood.do")
    @ResponseBody
    public R getOneGood(@RequestBody String params){
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String proId = paramsJSONObject.getString("proId");
        return R.success(goodService.getByProId(proId));
    }

    //更新商品
    @Login
    @PostMapping("/admin/updataGood.do")
    @ResponseBody
    public R getOneGood(@RequestBody ProductEntity productEntity){
        return R.success(goodService.updataGood(productEntity));
    }
}
